package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.Page;
import com.bdqn.pojo.Log;
import com.bdqn.service.LogService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 日志管理控制器
 */
@Controller
@RequestMapping("/admin/log")
public class LogController {

    @Resource
    private LogService logService;

    /**
     * 日志列表页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
       model.setViewName("log/list");
       return model;
    }

    /**
     * 获取日志列表
     * @param page
     * @param content
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(Page page, @RequestParam(name = "content", required = false,defaultValue = "")String content){
        Map<String,Object> ret = new HashMap<>();
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("content",content);
        queryMap.put("offset",page.getOffset());
        queryMap.put("pageSize",page.getRows());
        ret.put("rows",logService.findList(queryMap));
        ret.put("total",logService.getTotal(queryMap));
        return ret;
    }


    /**
     * 添加日志
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(Log log) {
        Map<String, String> ret = new HashMap<String, String>();
        if (log == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的日志信息！");
            return JSON.toJSONString(ret);
        }
        if (StringUtils.isEmpty(log.getContent())) {
            ret.put("type", "error");
            ret.put("msg", "请填写日志内容！");
            return JSON.toJSONString(ret);
        }
        log.setCreateTime(new Date());
        if (logService.add(log) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "日志添加失败，请联系管理员！");
            return JSON.toJSONString(ret);
        }
        ret.put("type", "success");
        ret.put("msg", "日志添加成功！");
        return JSON.toJSONString(ret);
    }

    /**
     * 批量删除日志
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(String ids) {
        Map<String, String> ret = new HashMap<String, String>();
        if (StringUtils.isEmpty(ids)) {
            ret.put("type", "error");
            ret.put("msg", "选择要删除的数据！");
            return JSON.toJSONString(ret);
        }
        if (ids.contains(",")) {
            ids = ids.substring(0, ids.length() - 1);
        }
        if (logService.delete(ids) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "日志删除失败，请联系管理员！");
            return JSON.toJSONString(ret);
        }
        ret.put("type", "success");
        ret.put("msg", "日志删除成功！");
        return JSON.toJSONString(ret);
    }

}
